package com.cn.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.cn.domain.Comment;
import com.cn.domain.Product;
import com.cn.domain.User;
import com.cn.service.CommentService;
import com.cn.service.ProductCategoryService;
import com.cn.service.UserService;
import com.cn.service.impl.CommentServiceImpl;
import com.cn.service.impl.ProductCategoryServiceImpl;
import com.cn.service.impl.UserServiceImpl;
import com.cn.utils.PageView;

@WebServlet("/ProductSortServlet")
public class ProductSortServlet extends HttpServlet {
	private ProductCategoryService pcs=new ProductCategoryServiceImpl();
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doGet(req, resp);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String type=req.getParameter("cate");
		String hpcId=req.getParameter("hpcId");
		System.out.println("type="+type+":hpcId="+hpcId);
		if("".equals(type)||"".equals(hpcId)) {
			hpcId="1";//父节点
		}
		//当前总记录条数
		int currentPage=1;
		String pageNow=req.getParameter("pageNow");
		System.out.println("pageNow="+pageNow);
		if(pageNow!=null) {
			currentPage=Integer.parseInt(pageNow);
		}
		List<Product> productList=pcs.queryProductById(type,hpcId,currentPage,PageView.PAGE_SIZE);
		
		System.out.println("type="+type);
		System.out.println("hpcId="+hpcId);
		//获取总条数
		List<Product> productList2=pcs.queryAllProductById(type,hpcId);
		System.out.println("proList2="+productList2);
		//这里进行分页显示
		PageView pageView=new PageView(currentPage, productList2.size(), productList);
		
		if("0".equals(type)){
			pageView.setStatus(0);
		}else if("1".equals(type)){
			pageView.setStatus(1);
		}
		req.setAttribute("pageModel", pageView);
		req.getRequestDispatcher("/index.jsp").forward(req, resp);
	}
}
